package com.ihm.projetihm_v1.nf.communication;

import java.io.File;
import java.io.FileOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class CommunicationFS
{
	//+++++++++++//
	// CONSTANTS //
	//+++++++++++//

	//++++++++++++//
	// ATTRIBUTES //
	//++++++++++++//
	
	private File mRootDirectory;

	
	//++++++++++++++//
	// CONSTRUCTORS //
	//++++++++++++++//

	/**
	 * Create root directory if not exists
	 * @param root 
	 *  Environment.getExternalStorageDirectory().getPath()
	 *  + "/Android/data/"
	 *  + packageName
	 *  + "/files/" <br/>
	 *  @see Context#getPackageName() Context.getApplicationContext().getPackageName()
	 * @throws Exception if directories cannot be created
	 */
	public CommunicationFS(String root)
			throws Exception
	{
		this.mRootDirectory = new File(root);
		if ( ! this.mRootDirectory.mkdirs())
		{
			if ( ! this.mRootDirectory.isDirectory())
			{
				throw new Exception("Directory cannot be created > " + root);
			}
		}
	}
	
	
	//+++++++++//
	// METHODS //
	//+++++++++//

	/**
	 * create a directory in root directory
	 * @param directoryName
	 * @throws Exception
	 */
	public void createDirectory(
			String directoryName
			)
			throws Exception
	{
		File f = new File(this.mRootDirectory, directoryName);
		
		if ( ! f.mkdir() && !f.isDirectory())
		{
			throw new Exception(
					"The directory <"
					+ f.getName()
					+ "> cannot be created.");
		}
	}

	/**
	 * remove a directory with its content from root directory
	 * @param directoryName
	 * @throws Exception if directory or its content cannot be removed
	 */
	public void removeDirectory(
			String directoryName
			)
			throws Exception
	{
		File f = new File(this.mRootDirectory, directoryName);
		
		File[] files = f.listFiles();
		for (File file : files)
		{
			if (file.isDirectory())
			{
				removeDirectory(file.getName());
			}
			if ( ! file.delete())
			{
				throw new Exception(
						"The file/directory <"
						+ file.getName()
						+ "> cannot be removed from folder <"
						+ directoryName
						+ ">.");
			}
		}
		
	}
	
	/**
	 * save an image from a bitmap, in a file in a specified directory
	 * in root directory
	 * @param bitmap
	 * @param directoryName
	 * @param fileName
	 * @throws Exception
	 */
	public void createBitmapInDirectory(
			Bitmap bitmap,
			String directoryName,
			String fileName
			)
			throws Exception
	{
		File file = new File(this.mRootDirectory, directoryName);
		file = new File(file, fileName);
		
		if ( ! file.createNewFile())
		{
			throw new Exception("file already exists");
		}
		
		FileOutputStream out = new FileOutputStream(file);
		bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
	}
	
	/**
	 * return a bitmap from a file in a directory of root directory
	 * @param directoryName
	 * @param fileName
	 * @return a bitmap from a file in a directory
	 */
	public Bitmap getBitmap(
			String directoryName,
			String fileName
			)
	{
		File f = new File(this.mRootDirectory, directoryName);
		f = new File(f, fileName);
		
		String imageFile = f.getAbsolutePath();
		
		return BitmapFactory.decodeFile(imageFile);
	}
	
	/**
	 * remove a file from a directory of root directory
	 * @param directoryName
	 * @param fileName
	 * @throws Exception if file cannot be removed
	 */
	public void removeFileFromDirectory(
			String directoryName,
			String fileName
			)
			throws Exception
	{
		File f = new File(this.mRootDirectory, directoryName);
		f = new File(f, fileName);
		
		if ( ! f.delete())
		{
			throw new Exception(
					"The file <"
					+ fileName
					+ "> cannot be removed from folder <"
					+ directoryName
					+ ">.");
		}
	}
	
	/**
	 * return an array of files contained into a directory of root directory
	 * @param directoryName
	 * return an array of files contained into a directory of root directory
	 */
	public File[] getContentDirectory(
			String directoryName
			)
	{
		File f = new File(this.mRootDirectory, directoryName);
		
		return f.listFiles();
	}
	
	/**
	 * return true if and only if the directory exists in root directory
	 * @param directoryName
	 * @return true if and only if the directory exists in root directory
	 */
	public boolean isExistingDirectory(
			String directoryName
			)
	{
		File f = new File(this.mRootDirectory, directoryName);
		
		return f.exists();
	}
	
	/**
	 * return true if and only if the file exists into a directory
	 * of root directory
	 * @param directoryName
	 * @param fileName
	 * @return true if and only if the file exists into a directory
	 * of root directory
	 */
	public boolean isExistingFileInDirectory(
			String directoryName,
			String fileName
			)
	{
		File f = new File(this.mRootDirectory, directoryName);
		f = new File(f, fileName);
		
		return f.exists();
	}
}
